<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme

  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100px'
      },
      chartData: {}
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            left: 'center',
            bottom: '10',
            data: this.chartData.projects
          },
          series: this.buildSeries()
        })
      },
      //看来这个饼状图还是不适用于多组数据
      buildSeries() {
        let series = [];
        for (let i = 0; i < this.chartData.items.length; i++) {
          series.push({
            name: this.chartData.items[i].name,
            type: 'pie',
            roseType: 'radius',
            radius: this.chartData.items[i].radius,
            center: ['50%', '38%'],
            data: this.chartData.items[i].dataList,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          })
        }
        return series;
      }
    }
  }
</script>
